#!/usr/bin/python
# Author: Yuan Li
# Info: 15110 lab1
# Last Modified date: july 6 2011
# ------------------------------------------------------------------
def areComplementary(strand1, strand2):

	# input validance checking
	if len(strand1) != len(strand2):
		return False
	
	# iterate checking pairness
	index = 0
	for index in range(len(strand1)):

		# matching formuler
		if strand1[index] == "A":
			if strand2[index] != "T":
				return False

		elif strand1[index] == "T":
			if strand2[index] != "A":
				return False
		
		elif strand1[index] == "G":
			if strand2[index] != "C":
				return False

		elif strand1[index] == "C":
			if strand2[index] != "G":
				return False	
		
		else:
			# malformed character 
			return False		

	# pass all the test, they are complementary!!!
	return True

def findTarget(strand, target):

	# input validation
	if strand == "":
		return -1
	if target == "":
		return -1


	strandIndex = 0	
	for strandIndex in range(len(strand)):

		targetIndex = 0
		# finding matching point
		if	charCompare(strand[strandIndex],target[targetIndex]) != True: 
		# not match? maybe try next chararctor in the strandIndex
			continue
		else:
			# target has only one character? job done!!
			if len(target) == 1:
				return strandIndex

			targetIndex = targetIndex + 1
			while 1:
				# run out of strand, but target not
				if (strandIndex + targetIndex) >= len(strand):
					break

				# there is different chararcter? dead, go back to find
				# another matching point
				if charCompare(strand[strandIndex+targetIndex],target[targetIndex]) !=True:
					break

				# push the index of target
				targetIndex = targetIndex + 1
			
				# hit the boundary of the target? job done!!
				if targetIndex >= len(target):
					return strandIndex

	# traverse all the strand, but do not find out any matching
	return -1

def charCompare(strand,target):
	
	if target == ".":
		return True

	if target == "x":
		if strand == "A" or strand == "T":
			return True

	if target == "y":
		if strand == "G" or strand == "C":
			return True

	# normal compare
	if strand == target:
		return True

	#cannot match any
	return False



def targetAtIndex(strand, target, i):
	if i == findTarget(strand,target):
		return True
	else:
		return False

def testAreComplementary():
    print "Testing areComplementary..."
    assert(areComplementary("A", "T") == True)
    assert(areComplementary("CTAGG", "GATCC") == True)
    assert(areComplementary("CTA", "GATT") == False)
    assert(areComplementary("CTACGC", "GAT") == False)
    assert(areComplementary("GGcT", "CCGA") == False)
    print "All tests passed!"

def testAtIndex():
    print "Testing targetAtIndex..."
    assert(targetAtIndex("A","A",0) == True)
    assert(targetAtIndex("AAAT","AT",2) == True)
    assert(targetAtIndex("AAAT","xT",2) == True)
    assert(targetAtIndex("GCCATA","yA.",2) == True)
    assert(targetAtIndex("GCCATA","TAC",4) == False)
    print "All tests passed!"

def testFindTarget():
    print "Testing findTarget..."
    assert(findTarget("A", "A") == 0)
    assert(findTarget("ATCGGA", "GAT") == -1)
    assert(findTarget("ATACGTG","ACGT") == 2)
    assert(findTarget("TTAGTTA","xyT.") == 2)
    print "All tests passed!"

testAreComplementary()
testAtIndex()
testFindTarget()
